﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Text;
using Jumbot.Common;
using Jumbot.DataOper;
using JumbotCms.DataObject;
namespace JumbotCms.API
{
    /// <summary>
    /// html页使用
    /// </summary>
    public class AdminCenter : JumbotCms.Common.BasicPage
    {
        public int publicMenu = 6;
        public string ChannelId = "0";
        public string ChannelName = string.Empty;
        public string ChannelType = "system";
        public string ChannelDir = string.Empty;
        public string ChannelItemName = string.Empty;
        public string ChannelItemUnit = string.Empty;
        public int ChannelClassDepth = 0;
        public bool ChannelIsHtml = false;
        public string id = "0";
        protected string AdminId = "0";
        protected string AdminName = string.Empty;
        protected string AdminPass = string.Empty;
        protected string AdminPower = string.Empty;
        protected string AdminCookiess = string.Empty;
        protected bool AdminIsLogin = false;
        protected bool AdminIsFounder = false;
        /// <summary>
        /// 列表内容通用方法,必须重写
        /// </summary>
        protected virtual void getListBox() { }
        /// <summary>
        /// 编辑内容通用方法,必须重写
        /// </summary>
        protected virtual void editBox() { }
        /// <summary>
        /// 验证登陆
        /// </summary>
        private void chkLogin()
        {
            if (Cookie.GetValue(site.CookiePrev + "admin") != null)
            {
                AdminId = Cookie.GetValue(site.CookiePrev + "admin", "id");
                AdminName = Cookie.GetValue(site.CookiePrev + "admin", "name");
                AdminPass = Cookie.GetValue(site.CookiePrev + "admin", "password");
                AdminCookiess = Cookie.GetValue(site.CookiePrev + "admin", "cookies");
                if (AdminId.Length != 0 && AdminName.Length != 0)
                {
                    doh.Reset();
                    doh.ConditionExpress = "adminid=@id and cookiess=@cookiess";//禁止一个用户同时登录
                    doh.AddConditionParameter("@id", AdminId);
                    doh.AddConditionParameter("@cookiess", AdminCookiess);
                    AdminPower = doh.GetValue("jcms_normal_user", "Setting").ToString();
                    if (AdminPower.Length != 0)
                    {
                        this.AdminIsLogin = true;
                        string Founders = Jumbot.Common.XmlCOM.ReadConfig("~/_data/config/config", "Founders");
                        //if (Founders.IndexOf("." + AdminName + ".") < 0)
                        //    this.AdminIsFounder = false;
                        //else
                        //    this.AdminIsFounder = true;
                        this.AdminIsFounder = (Founders.ToLower().Contains("." + AdminName.ToLower() + "."));
                    }
                }
            }
        }
        /// <summary>
        /// 验证权限
        /// 超级管理员永远有效
        /// </summary>
        /// <param name="s">空时只要求登录</param>
        protected bool IsPower(string s)
        {
            if (!this.AdminIsLogin)//验证一次本地信息
                chkLogin();
            if (s == "") return (this.AdminIsLogin);
            if (this.AdminIsFounder) return (this.AdminIsLogin);
            //if (this.AdminPower.IndexOf("," + s + ",") < 0)
            //    return false;
            //else
            //    return true;
            return (this.AdminPower.Contains("," + s + ","));
        }
        /// <summary>
        /// 验证权限
        /// 超级管理员永远有效
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pageType">页面分为html和json</param>
        protected void chkPower(string s, string pageType)
        {
            if (!IsPower(s))
            {
                showErrMsg("权限不足或未登录", pageType);
            }
        }
        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="pageType">页面分为html和json</param>
        protected void showErrMsg(string msg, string pageType)
        {
            if (pageType != "json")
                FinalMessage(msg, site.Dir + "admin/login.htm", 0);
            else
            {
                HttpContext.Current.Response.Clear();
                if (!this.AdminIsLogin)
                    HttpContext.Current.Response.Write(JsonResult(-1, msg));
                else
                    HttpContext.Current.Response.Write(JsonResult(0, msg));
                HttpContext.Current.Response.End();
            }
        }
        /// <summary>
        /// 管理中心初始
        /// </summary>
        /// <param name="powerNum">权限,为空表示验证是否登录</param>
        /// <param name="pageType">页面分为html和json</param>
        protected void Admin_Load(string powerNum, string pageType)
        {
            chkPower(powerNum, pageType);
        }

        /// <summary>
        /// 管理中心初始,并获得频道的各项参数值
        /// </summary>
        /// <param name="powerNum">权限</param>
        /// <param name="isChannel">如果为false就表示ChannelId可以为0</param>
        protected void Admin_Load(string powerNum, string pageType, bool isChannel)
        {
            chkPower(powerNum, pageType);
            if (isChannel && ChannelId == "0")
            {
                showErrMsg("参数错误,请不要在外部提交数据", pageType);
                return;
            }
            if (ChannelId != "0")
            {
                JumbotCms.DataObject.Channel _Channel = new JumbotCms.DataObject.Channel();
                _Channel.GetAllColumn(ChannelId, doh);
                ChannelName = _Channel.Title;
                ChannelDir = _Channel.Dir;
                ChannelType = _Channel.Type;
                ChannelItemName = _Channel.ItemName;
                ChannelItemUnit = _Channel.ItemUnit;
                ChannelClassDepth = _Channel.ClassDepth;
                ChannelIsHtml = _Channel.IsHtml;
            }
        }
        /// <summary>
        /// 管理菜单
        /// </summary>
        /// <returns></returns>
        protected string[,] leftMenu()
        {
            doh.Reset();
            doh.SqlCmd = "SELECT Id,Title,[Type] FROM [jcms_normal_modules] WHERE [Enabled]=1 ORDER BY pId";
            DataTable dtModule = doh.GetDataTable();
            string[,] menu = new string[publicMenu + dtModule.Rows.Count, 30];
            menu[0, 0] = "系统管理$0$system";
            menu[0, 1] = site.Dir + "admin/configset/index.aspx|网站参数";
            menu[0, 2] = site.Dir + "admin/modules/list.htm|模块管理";
            menu[0, 3] = site.Dir + "admin/channel/list.htm|频道管理";
            menu[0, 4] = site.Dir + "admin/extends/list.htm|插件管理";
            menu[0, 5] = site.Dir + "admin/special/list.htm|专题管理";
            menu[0, 6] = site.Dir + "admin/forbidip/list.htm|IP黑名单";
            menu[0, 7] = site.Dir + "admin/masterlogs/list.htm|管理日志";
            menu[0, 8] = site.Dir + "admin/executesql/index.aspx|在线执行SQL";
            menu[0, 9] = site.Dir + "admin/urlrewriter/index.aspx|伪静态规则";
            if (base.DBType == "0")
                menu[0, 10] = site.Dir + "admin/database/access.aspx|数据备份恢复";

            menu[1, 0] = "用户管理$0$member";
            menu[1, 1] = site.Dir + "admin/user/list.htm|" + site.UserItem + "管理";
            menu[1, 2] = site.Dir + "admin/user_apply/list.htm|" + site.UserItem + "申请单";
            menu[1, 3] = site.Dir + "admin/pointscard/list.htm|点卡管理";
            menu[1, 4] = site.Dir + "admin/usergroup/list.htm|用户组管理";
            menu[1, 5] = site.Dir + "admin/master/list.htm|管理员管理";
            if (base.DBType == "1")
                menu[1, 6] = site.Dir + "admin/user/api.aspx|整合接口";

            menu[2, 0] = "模板管理$0$template";
            menu[2, 1] = site.Dir + "admin/templatecontentli/list.htm|循环调用宏标记";
            menu[2, 2] = site.Dir + "admin/templateproject/list.htm|HTML模板方案";
            menu[2, 3] = site.Dir + "admin/skins/list.htm|CSS样式方案";
            menu[2, 4] = site.Dir + "admin/page_sort/list.htm|单页内容管理";
            menu[2, 5] = site.Dir + "admin/javascript/list.htm|自定义外站调用";

            menu[3, 0] = "后台首页$0$config";
            menu[3, 1] = site.Dir + "admin/home.htm|前台更新";
            menu[3, 2] = site.Dir + "admin/serverinfo/index.aspx|服务器探针";
            menu[3, 3] = site.Dir + "admin/myinfo/password.aspx|修改密码";

            menu[4, 0] = "邮件系统$0$mail";
            menu[4, 1] = site.Dir + "admin/mail/send.aspx|邮件群发";
            menu[4, 2] = site.Dir + "admin/mail/system.aspx|邮箱配置";

            //************外部频道开始******************//
            menu[5, 0] = "外部频道$0$outurl";

            doh.Reset();
            doh.SqlCmd = "SELECT Id,Title FROM [jcms_normal_channel] WHERE [Enabled]=1 AND [IsOut]=1 ORDER BY pId";
            DataTable dtChannel0 = doh.GetDataTable();
            string mChannelId0 = string.Empty;
            string mChannelName0 = string.Empty;
            for (int p = 0; p < dtChannel0.Rows.Count; p++)
            {
                mChannelId0 = dtChannel0.Rows[p]["Id"].ToString();
                mChannelName0 = dtChannel0.Rows[p]["Title"].ToString();
                menu[5, p + 1] = site.Dir + "admin/class/list2.htm?ccid=" + mChannelId0 + "&ctype=system|" + mChannelName0;
            }
            dtChannel0.Clear();
            dtChannel0.Dispose();
            //************外部频道结束******************//
            string mModuleId = string.Empty;
            string mModuleName = string.Empty;
            string mModuleType = string.Empty;
            int j = 1;
            for (int i = 0; i < dtModule.Rows.Count; i++)
            {
                j = i + publicMenu;
                mModuleId = dtModule.Rows[i]["Id"].ToString();
                mModuleName = dtModule.Rows[i]["Title"].ToString();
                mModuleType = dtModule.Rows[i]["Type"].ToString().ToLower();
                menu[j, 0] = mModuleName + "管理$1$m_" + mModuleType;
                doh.Reset();
                doh.SqlCmd = "SELECT Id,Title FROM [jcms_normal_channel] WHERE [Enabled]=1 AND [IsOut]<1 AND [Type]='" + mModuleType + "' ORDER BY pId";
                DataTable dtChannel = doh.GetDataTable();
                string mChannelId = string.Empty;
                string mChannelName = string.Empty;
                for (int m = 0; m < dtChannel.Rows.Count; m++)
                {
                    mChannelId = dtChannel.Rows[m]["Id"].ToString();
                    mChannelName = dtChannel.Rows[m]["Title"].ToString();
                    menu[j, m + 1] = site.Dir + "modules/" + mModuleType + "/admin/list.htm?ccid=" + mChannelId + "&ctype=" + mModuleType + "|" + mChannelName + "|" + mChannelId;
                }
                dtChannel.Clear();
                dtChannel.Dispose();
            }
            dtModule.Clear();
            dtModule.Dispose();
            return menu;
        }

        /// <summary>
        /// 编辑内容时,向栏目专题标题颜色等DropDownList中添加内容
        /// </summary>
        /// <param name="ddlClassId">栏目ID</param>
        /// <param name="ClassDepth">栏目深度</param>
        /// <param name="ddlReadGroup">阅读权限</param>
        protected void getEditDropDownList(ref DropDownList ddlClassId, int ClassDepth, ref DropDownList ddlReadGroup)
        {
            if (!Page.IsPostBack)
            {
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[Title],[code] FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + ChannelId;
                if (ClassDepth > 0)
                    doh.SqlCmd += " AND left(code)<=" + (4 * ClassDepth).ToString();
                doh.SqlCmd += " ORDER BY code";
                DataTable dtClass = doh.GetDataTable();
                if (dtClass.Rows.Count == 0)
                {
                    dtClass.Clear();
                    dtClass.Dispose();
                    Response.Write("请先添加栏目!");
                    Response.End();
                    return;
                }
                for (int i = 0; i < dtClass.Rows.Count; i++)
                {
                    ddlClassId.Items.Add(new ListItem(getListName(dtClass.Rows[i]["Title"].ToString(), dtClass.Rows[i]["code"].ToString()), dtClass.Rows[i]["Id"].ToString()));
                }
                dtClass.Clear();
                dtClass.Dispose();
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[GroupName] FROM [jcms_normal_usergroup] ORDER BY id ASC";
                DataTable dtGroup = doh.GetDataTable();
                ddlReadGroup.Items.Clear();
                ddlReadGroup.Items.Add(new ListItem("继承栏目权限", "-1"));
                ddlReadGroup.Items.Add(new ListItem("匿名游客", "0"));
                for (int i = 0; i < dtGroup.Rows.Count; i++)
                {
                    ddlReadGroup.Items.Add(new ListItem(dtGroup.Rows[i]["GroupName"].ToString(), dtGroup.Rows[i]["Id"].ToString()));
                }
                dtGroup.Clear();
                dtGroup.Dispose();
            }
        }
        /// <summary>
        /// 统计用户组用户总数
        /// <param name="_gId">用户组Id，为0表示更新所有的</param>
        /// </summary>
        protected void UserGroupCount(string _gId)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT ID FROM [jcms_normal_usergroup]";
            if (_gId != "0") doh.SqlCmd += " WHERE [Id]=" + _gId;
            DataTable dtUserGroup = doh.GetDataTable();
            int total = dtUserGroup.Rows.Count;
            int tmp;
            for (int i = 0; i < total; i++)
            {
                doh.Reset();
                doh.SqlCmd = "SELECT ID FROM [jcms_normal_user] WHERE [Group]=" + dtUserGroup.Rows[i]["Id"].ToString();
                tmp = doh.GetDataTable().Rows.Count;
                doh.Reset();
                doh.ConditionExpress = "id=" + dtUserGroup.Rows[i]["Id"].ToString();
                doh.AddFieldItem("UserTotal", tmp);
                doh.Update("jcms_normal_usergroup");
            }
            dtUserGroup.Clear();
            dtUserGroup.Dispose();
        }
        /// <summary>
        /// 保存管理日志
        /// </summary>
        /// <param name="_info">保存信息</param>
        protected void SaveAdminLog(string _info)
        {
            chkLogin();
            doh.Reset();
            doh.AddFieldItem("AdminId", AdminId);
            doh.AddFieldItem("OperInfo", _info);
            doh.AddFieldItem("OperTime", DateTime.Now.ToString());
            doh.AddFieldItem("OperIP", GetUserIp());
            doh.Insert("jcms_normal_adminlogs");
        }
        /// <summary>
        /// 取得内容列表
        /// </summary>
        /// <param name="_classId">栏目Id</param>
        /// <param name="keyType">搜索关键字类型{Author,title,summary}</param>
        /// <param name="keyWord">搜索关键字</param>
        /// <param name="sDate">日期{1d=今天,1w=本周,1m=本月}</param>
        /// <param name="isPass">是否审核{0=未审,1=已审,-1=已删,否则=全部}</param>
        /// <param name="isTop">是否推荐{0=不推荐,1=推荐,否则=全部}</param>
        /// <param name="PSize">每页记录数</param>
        /// <param name="page">页码</param>
        protected string GetContentList(string _ctype, string _classId, string keyType, string keyWord, string sDate, string isPass, string isTop, int PSize, int page)
        {
            _classId = Jumbot.Common.Strings.SafetyStr(_classId);
            keyType = Jumbot.Common.Strings.SafetyStr(keyType);
            keyWord = Jumbot.Common.Strings.SafetyStr(keyWord);
            sDate = Jumbot.Common.Strings.SafetyStr(sDate);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[ClassId]=B.Id";
            string whereStr1 = "A.ChannelId=" + ChannelId;//外围条件(带A.)
            string whereStr2 = "ChannelId=" + ChannelId;//分页条件(不带A.)
            if (_classId != "0")
            {
                whereStr1 += " AND A.ClassId=" + _classId;
                whereStr2 += " AND ClassId=" + _classId;
            }
            if ((keyType.Length > 0) && (keyWord.Length > 0))
            {
                whereStr1 += " and A." + keyType + " LIKE '%" + keyWord + "%'";
                whereStr2 += " and " + keyType + " LIKE '%" + keyWord + "%'";
            }
            switch (isPass)
            {
                case "0":
                    whereStr1 += " AND A.IsPass=0";
                    whereStr2 += " AND IsPass=0";
                    break;
                case "1":
                    whereStr1 += " AND A.IsPass=1";
                    whereStr2 += " AND IsPass=1";
                    break;
                case "-1":
                    whereStr1 += " AND A.IsPass=-1";
                    whereStr2 += " AND IsPass=-1";
                    break;
                default:
                    break;
            }
            switch (isTop)
            {
                case "0":
                    whereStr1 += " AND A.IsTop=0";
                    whereStr2 += " AND IsTop=0";
                    break;
                case "1":
                    whereStr1 += " AND A.IsTop=1";
                    whereStr2 += " AND IsTop=1";
                    break;
                default:
                    break;
            }
            if (sDate != "")
            {
                if (DBType == "0")
                {
                    switch (sDate)
                    {
                        case "1d":
                            whereStr1 += " AND datediff('d',A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff('d',adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1w":
                            whereStr1 += " AND datediff('ww',A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff('ww',adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1m":
                            whereStr1 += " AND datediff('m',A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff('m',adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                    }
                }
                else
                {
                    switch (sDate)
                    {
                        case "1d":
                            whereStr1 += " AND datediff(d,A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff(d,adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1w":
                            whereStr1 += " AND datediff(ww,A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff(ww,adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1m":
                            whereStr1 += " AND datediff(m,A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff(m,adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                    }
                }
            }

            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_module_" + _ctype);
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.id as id,A.channelid as channelid,A.Title as title,B.Title as classname,A.adddate as adddate,A.IsPass as IsPass,A.OutUrl as OutUrl", "jcms_module_" + _ctype, "jcms_normal_class", "id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            return "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "returmodule :\"" + _ctype + "\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
        }
        protected void CreateMenu(string _channelID)
        {
            string siteurl = string.Empty;
            if (_channelID == "0")
            {
                string ChannelRootClassStr = "var $$_JSON_SystemTree = {";
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[Title],[Dir],[Target],[IsOut],[OutUrl],[Type],[IsNav],[IsHtml] FROM [jcms_normal_channel] WHERE [Enabled]=1 ORDER BY pId";
                DataTable dtChannel = doh.GetDataTable();
                ChannelRootClassStr += "recordcount: " + dtChannel.Rows.Count + ", " +
                    "table: [";
                if (dtChannel.Rows.Count > 0)
                {
                    for (int c = 0; c < dtChannel.Rows.Count; c++)
                    {
                        string _ccId = dtChannel.Rows[c]["Id"].ToString();
                        string _ccTitle = dtChannel.Rows[c]["Title"].ToString();
                        string _ccDir = dtChannel.Rows[c]["Dir"].ToString();
                        string _ccIsNav = dtChannel.Rows[c]["IsNav"].ToString();
                        string _ccIsOut = dtChannel.Rows[c]["IsOut"].ToString();
                        string _ccType = dtChannel.Rows[c]["Type"].ToString();
                        string _target = "_self";
                        if (dtChannel.Rows[c]["Target"].ToString() == "1") _target = "_blank";
                        if (c > 0) ChannelRootClassStr += ",";
                        siteurl = Go2Channel(true, _ccId);
                        ChannelRootClassStr += "{" +
                            "no: " + c + ", " +
                            "id: " + _ccId + ", " +
                            "isnav: " + _ccIsNav + ", " +
                            "title: '" + _ccTitle + "', " +
                            "isout: " + _ccIsOut + ", " +
                            "url: '" + siteurl + "', " +
                            "target: '" + _target + "', " +
                            "type: '" + _ccType.ToLower() + "', ";
                        ChannelRootClassStr += CreateClassLi((dtChannel.Rows[c]["IsHtml"].ToString() == "1"), _ccId, "0");

                        ChannelRootClassStr += "}";
                    }
                }
                ChannelRootClassStr += "]";
                ChannelRootClassStr += "}";
                Jumbot.Common.DirFile.SaveFile(ChannelRootClassStr, "~/_data/json/_systemtree.js");
            }
        }
        protected string CreateClassLi(bool _isHtml, string _ccID, string _classId)
        {

            string tempStr = string.Empty;
            doh.Reset();
            if (_classId != "0")
                doh.SqlCmd = "SELECT ID,Title,ParentId,[Code],[IsTop] FROM [jcms_normal_class] WHERE [ChannelId]=" + _ccID + " AND [ParentId]=" + _classId + " order by code";
            else
                doh.SqlCmd = "SELECT ID,Title,ParentId,[Code],[IsTop] FROM [jcms_normal_class] WHERE [ChannelId]=" + _ccID + " AND [ParentId]=0 order by code";
            DataTable dtClassSub = doh.GetDataTable();
            tempStr += "recordcount: " + dtClassSub.Rows.Count + ", " +
                            "table: [";
            if (dtClassSub.Rows.Count > 0)
            {
                for (int i = 0; i < dtClassSub.Rows.Count; i++)
                {

                    if (i > 0)
                        tempStr += ",";
                    tempStr += "{" +
                        "no: " + i + ", " +
                        "id: " + dtClassSub.Rows[i]["Id"].ToString() + ", " +
                        "istop: " + dtClassSub.Rows[i]["IsTop"].ToString() + ", " +
                        "title: '" + dtClassSub.Rows[i]["Title"].ToString() + "', " +
                        "url: '" + Go2Class(doh, 1, _isHtml, _ccID, dtClassSub.Rows[i]["Id"].ToString()) + "', " +
                        "rssurl: '" + Go2Rss(doh, 1, _isHtml, _ccID, dtClassSub.Rows[i]["Id"].ToString()) + "'," +
                        CreateClassLi(_isHtml, _ccID, dtClassSub.Rows[i]["Id"].ToString()) +
                        "}";
                }
            }
            tempStr += "]";
            return tempStr;
        }
        /// <summary>
        /// 统计数据
        /// </summary>
        /// <param name="_channelID"></param>
        protected void CreateCount(string _channelID)
        {
            int _classCount = 0;
            int _contentCount = 0;
            if (_channelID == "0")
            {
                string TempCountStr = "var $$_JSON_SystemCount = {";
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[Title],[Type] FROM [jcms_normal_channel] WHERE [Enabled]=1 and [IsOut]=0 ORDER BY pId";
                DataTable dtChannel = doh.GetDataTable();
                TempCountStr += "table: [";
                if (dtChannel.Rows.Count > 0)
                {
                    for (int c = 0; c < dtChannel.Rows.Count; c++)
                    {
                        if (c > 0) TempCountStr += ",";
                        string _ccId = dtChannel.Rows[c]["Id"].ToString();
                        string _ccTitle = dtChannel.Rows[c]["Title"].ToString();
                        string _ccType = dtChannel.Rows[c]["Type"].ToString();
                        doh.Reset();
                        doh.ConditionExpress = "ChannelId=" + _ccId + " and ParentId=0";
                        _classCount = doh.Count("jcms_normal_class");
                        doh.Reset();
                        doh.ConditionExpress = "ChannelId=" + _ccId + " and IsPass=1";
                        _contentCount = doh.Count("jcms_module_" + _ccType);
                        TempCountStr += "{" +
                            "channelid: " + _ccId + "," +
                            "classid: 0," +
                            "title: '" + _ccTitle + "'," +
                            "classcount: " + _classCount + "," +
                            "contentcount: " + _contentCount + "}";

                        doh.Reset();
                        doh.SqlCmd = "SELECT Id,Title,[Code] FROM [jcms_normal_class] WHERE [ParentId]=0 AND [ChannelId]=" + _ccId + " ORDER BY code";
                        DataTable dtClass = doh.GetDataTable();
                        if (dtClass.Rows.Count > 0)
                        {
                            for (int i = 0; i < dtClass.Rows.Count; i++)
                            {
                                string _cId = dtClass.Rows[i]["Id"].ToString();
                                string _cTitle = dtClass.Rows[i]["Title"].ToString();
                                string _cCode = dtClass.Rows[i]["Code"].ToString();
                                doh.Reset();
                                doh.ConditionExpress = "ChannelId=" + _ccId + " and ParentId=" + _cId;
                                _classCount = doh.Count("jcms_normal_class");
                                doh.Reset();
                                doh.ConditionExpress = "ChannelId=" + _ccId + " and IsPass=1 And [ClassID] in (SELECT ID FROM [jcms_normal_class] WHERE [IsOut]=0 AND [Code] Like '" + _cCode + "%')";
                                _contentCount = doh.Count("jcms_module_" + _ccType);
                                TempCountStr += "," + "{" +
                                    "channelid: " + _ccId + "," +
                                    "classid: " + _cId + "," +
                                    "title: '" + _cTitle + "'," +
                                    "classcount: " + _classCount + "," +
                                    "contentcount: " + _contentCount + "}";
                            }
                        }
                    }
                }
                TempCountStr += "]";
                TempCountStr += "}";
                Jumbot.Common.DirFile.SaveFile(TempCountStr, "~/_data/json/_systemcount.js");
            }
        }
        protected void CreateNavigate(string _ccID)
        {
            Channel _Channel = new Channel();
            string _ccType = _Channel.GetType(_ccID, doh);
            doh.Reset();
            doh.SqlCmd = "SELECT ID FROM [jcms_normal_class] WHERE [ChannelId]=" + _ccID + " ORDER BY code";
            DataTable dtClass = doh.GetDataTable();
            if (dtClass.Rows.Count > 0)
            {
                for (int i = 0; i < dtClass.Rows.Count; i++)
                {
                    string _classId = dtClass.Rows[i]["Id"].ToString();
                    CreateClassNavigate(_ccID, _classId);
                }
            }
            dtClass.Clear();
            dtClass.Dispose();
        }
        protected void CreateClassNavigate(string _ccID, string _classId)
        {
            Channel _Channel = new Channel();
            string _ccName = _Channel.GetTitle(_ccID, doh);
            System.Text.StringBuilder sb_js = new System.Text.StringBuilder();
            sb_js.Append("document.write('<a href=\"" + site.Dir + "\">" + site.Name + "</a>");
            sb_js.Append("&nbsp;&gt;&nbsp;<a href=\"" + Go2Channel(site.IsHtml, _ccID) + "\">" + _ccName + "</a>");
            sb_js.Append(NavigateHtml(site.IsHtml, _ccID, _classId));
            sb_js.Append("');");
            string js = "";
            doh.Reset();
            doh.SqlCmd = "SELECT ID,Title,dir,target,IsOut,OutUrl FROM [jcms_normal_channel] WHERE [Enabled]=1 AND [Id]=" + _ccID;
            DataTable dtChannel = doh.GetDataTable();
            js = Server.MapPath(site.Dir) + dtChannel.Rows[0]["dir"].ToString() + "\\js\\classnav_" + _classId + ".js";
            SaveJsFile(sb_js.ToString(), js);
            dtChannel.Clear();
            dtChannel.Dispose();
        }
        /// <summary>
        /// 获得内容移动栏目导航html
        /// </summary>
        /// <param name="_ccID"></param>
        /// <param name="_classId"></param>
        /// <returns></returns>
        protected string Move2ClassNavHtml(string _ccID, string _classId)
        {
            string tempStr = string.Empty;
            if (_classId == "0")
                tempStr += "\r\n<ul class=\"sub\">\r\n";
            else
                tempStr += "\r\n<ul>\r\n";
            doh.Reset();
            if (_classId != "0")
            {
                doh.SqlCmd = "SELECT ID,Title,ParentId,[Code] FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + _ccID + " AND [ParentId]=" + _classId + " order by code";
            }
            else
                doh.SqlCmd = "SELECT ID,Title,ParentId,[Code] FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + _ccID + " AND [ParentId]=0 order by code";

            DataTable dtClassSub = doh.GetDataTable();
            for (int i = 0; i < dtClassSub.Rows.Count; i++)
            {
                if (new JumbotCms.DataObject.Class().HasChild(dtClassSub.Rows[i]["Id"].ToString(), doh))
                    tempStr += "<li><a href=\"javascript:void(0);\" onclick=\"move2class(" + dtClassSub.Rows[i]["Id"].ToString() + ");\" class=\"fly\">" + dtClassSub.Rows[i]["Title"].ToString() + "</a>" + Move2ClassNavHtml(_ccID, dtClassSub.Rows[i]["Id"].ToString()) + "</li>\r\n";

                else
                    tempStr += "<li><a href=\"javascript:void(0);\" onclick=\"move2class(" + dtClassSub.Rows[i]["Id"].ToString() + ");\">" + dtClassSub.Rows[i]["Title"].ToString() + "</a></li>\r\n";

            }
            tempStr += "</ul>\r\n";
            return tempStr;
        }
        /// <summary>
        /// 获得栏目导航html
        /// </summary>
        /// <param name="ishtml"></param>
        /// <param name="_channelID"></param>
        /// <param name="_classId"></param>
        /// <returns></returns>
        protected string NavigateHtml(bool ishtml, string _ccID, string _classId)
        {
            Channel _Channel = new Channel();
            string _ccType = _Channel.GetType(_ccID, doh);
            doh.Reset();
            doh.SqlCmd = "SELECT ID,Title,ParentId,[Code] FROM [jcms_normal_class] WHERE [ChannelId]=" + _ccID + " AND [Id]=" + _classId;
            DataTable dtClass = doh.GetDataTable();
            string ParentId = dtClass.Rows[0]["ParentId"].ToString();
            string ClassName = dtClass.Rows[0]["Title"].ToString();
            dtClass.Clear();
            dtClass.Dispose();
            if (ParentId == "0")
            {
                return "&nbsp;&gt;&nbsp;<a href=\"" + Go2Class(doh, 1, _Channel.IsHtml, _ccID, _classId) + "\">" + ClassName + "</a>";
            }
            else
            {
                return NavigateHtml(ishtml, _ccID, ParentId) + "&nbsp;&gt;&nbsp;<a href=\"" + Go2Class(doh, 1, _Channel.IsHtml, _ccID, _classId) + "\">" + ClassName + "</a>";
            }
        }
        /// <summary>
        /// 执行内容的移动,审核,删除等操作
        /// </summary>
        /// <param name="_act">操作类型{pass=审核,nopass=未审,remove=移入回收站,del=彻底删除}</param>
        /// <param name="_ids">id字符串,以","串联起来</param>
        /// <param name="pageType">页面分为html和json</param>
        public void BatchContent(string _act, string _tocid, string _ids, string _ccId, string _cType, string pageType)
        {
            string[] idValue;
            idValue = _ids.Split(',');
            if (_act == "createhtml")
            {
                Admin_Load(_ccId + "-04", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    if (ChannelIsHtml)
                        CreateContentFile(idValue[i], -1);//生成内容页
                }
                return;
            }
            if (_act == "pass")
            {
                Admin_Load(_ccId + "-04", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i] + " and [IsPass]<1";
                    doh.AddFieldItem("IsPass", 1);
                    if (doh.Update("jcms_module_" + _cType) == 1)
                    {
                        if (ChannelIsHtml)
                            CreateContentFile(idValue[i], -1);//生成内容页
                    }
                }
                return;
            }
            if (_act == "nopass")
            {
                Admin_Load(_ccId + "-04", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i] + " and [IsPass]=1";
                    doh.AddFieldItem("IsPass", 0);
                    if (doh.Update("jcms_module_" + _cType) == 1)
                    {
                        if (ChannelIsHtml)
                            DeleteContentFile(idValue[i]);//删内容页
                    }
                }
                return;
            }
            if (_act == "top")
            {
                Admin_Load(_ccId + "-05", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i];
                    doh.AddFieldItem("IsTop", 1);
                    doh.Update("jcms_module_" + _cType);
                }
                return;
            }
            if (_act == "notop")
            {
                Admin_Load(_ccId + "-05", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i];
                    doh.AddFieldItem("IsTop", 0);
                    doh.Update("jcms_module_" + _cType);
                }
                return;
            }
            if (_act == "move2class")
            {
                Admin_Load(_ccId + "-06", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i];
                    doh.AddFieldItem("ClassId", _tocid);
                    doh.Update("jcms_module_" + _cType);
                    if (ChannelIsHtml)
                        CreateContentFile(idValue[i], -1);//生成内容页
                }
                return;
            }
            if (_act == "sdel")
            {
                Admin_Load(_ccId + "-03", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    if (ChannelIsHtml)
                        DeleteContentFile(idValue[i]);//删内容页
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i];
                    doh.AddFieldItem("IsPass", -1);
                    doh.Update("jcms_module_" + _cType);//放入回收站
                }
                return;
            }
            if (_act == "del")
            {
                Admin_Load("0000", "json");
                for (int i = 0; i < idValue.Length; i++)
                {
                    if (ChannelIsHtml)
                        DeleteContentFile(idValue[i]);//先删内容页
                    doh.Reset();
                    doh.ConditionExpress = "id=" + idValue[i];
                    doh.Delete("jcms_module_" + _cType);
                }
                return;
            }
        }
        /// <summary>
        /// 删除站内导航文件
        /// </summary>

        protected void DeleteSiteMapFile()
        {
            Jumbot.Common.DirFile.DeleteFile(site.Dir + "sitemap" + site.StaticExt);
        }
        #region 生成静态页面
        /// <summary>
        /// 删除首页文件
        /// </summary>

        protected void CreateDefaultFile()
        {
            string pId = string.Empty;
            string sId = string.Empty;
            string PageStr = string.Empty;
            doh.Reset();
            doh.SqlCmd = "SELECT TOP 1 [pid],[Source],[SkinId] FROM [jcms_normal_template] WHERE [Type]='System' AND [sType]='Index' ORDER BY IsDefault desc";
            DataTable dtTemplate = doh.GetDataTable();
            if (dtTemplate.Rows.Count > 0)
            {
                pId = dtTemplate.Rows[0]["pid"].ToString();
                sId = dtTemplate.Rows[0]["SkinId"].ToString();
                TemplateProject tp = new TemplateProject();
                PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/" + tp.GetDir(pId, doh) + "/" + dtTemplate.Rows[0]["Source"].ToString());
            }
            dtTemplate.Clear();
            dtTemplate.Dispose();
            TemplateEngine te = new TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.PageNav = site.Name + "&nbsp;&gt;&nbsp;首页";
            te.PageTitle = site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = site.Description;
            te.ReplacePublicTag(ref PageStr, pId, sId);
            te.ReplaceRepeatTag(ref PageStr);
            Jumbot.Common.DirFile.SaveFile(PageStr, "~/index" + site.StaticExt);

        }
        /// <summary>
        /// 生成频道首页
        /// </summary>
        protected void CreateChannelFile()
        {
            CreateChannelFile(ChannelId);
        }
        protected void CreateChannelFile(string _channelID)
        {
            JumbotCms.DataObject.Channel _Channel = new JumbotCms.DataObject.Channel();
            _Channel.GetAllColumn(_channelID, doh);
            doh.Reset();
            doh.SqlCmd = "SELECT [Title],[Info],[ItemName],[ItemUnit],[TemplateId] FROM [jcms_normal_channel] WHERE [Id]=" + _Channel.Id + " AND [IsOut]<1";
            DataTable dtChannel = doh.GetDataTable();
            string TempId = dtChannel.Rows[0]["TemplateId"].ToString();
            string pId = string.Empty;
            string sId = string.Empty;
            string PageStr = string.Empty;
            dtChannel.Clear();
            dtChannel.Dispose();
            doh.Reset();
            doh.SqlCmd = "SELECT TOP 1 [pid],[Source],[SkinId] FROM [jcms_normal_template] WHERE [Id]=" + TempId;
            DataTable dtTemplate = doh.GetDataTable();
            if (dtTemplate.Rows.Count > 0)
            {
                pId = dtTemplate.Rows[0]["pid"].ToString();
                sId = dtTemplate.Rows[0]["SkinId"].ToString();
                TemplateProject tp = new TemplateProject();
                PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/" + tp.GetDir(pId, doh) + "/" + dtTemplate.Rows[0]["Source"].ToString());
            }
            dtTemplate.Clear();
            dtTemplate.Dispose();
            TemplateEngine te = new TemplateEngine(doh, _Channel.Id);
            te.IsHtml = site.IsHtml;
            te.PageNav = "<a href=\"/\">首页</a>&nbsp;&gt;&nbsp;" + _Channel.Title;
            te.PageTitle = _Channel.Title + "_" + site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = Jumbot.Common.Strings.SimpleLineSummary(_Channel.Info);
            te.ReplacePublicTag(ref PageStr, pId, sId);
            te.ReplaceChannelTag(ref PageStr, _channelID);
            te.ReplaceRepeatTag(ref PageStr);
            Jumbot.Common.DirFile.SaveFile(PageStr, site.Dir + _Channel.Dir + "/index" + site.StaticExt);
        }
        /// <summary>
        /// 删除栏目文件
        /// </summary>
        /// <param name="_classId"></param>
        /// <param name="DeleteParent"></param>
        protected void DeleteClassFile(string _classId, bool DeleteParent)
        {
            if (System.IO.Directory.Exists(Server.MapPath(site.Dir + "/" + ChannelDir)))
            {
                string htmFile = Server.MapPath(Go2Class(doh, 1, true, ChannelId, _classId));
                if (System.IO.File.Exists(htmFile))
                    System.IO.File.Delete(htmFile);
                string[] htmFiles = System.IO.Directory.GetFiles(Server.MapPath(site.Dir + "/" + ChannelDir), "class_" + _classId + "_*" + site.StaticExt);
                foreach (string fileName in htmFiles)
                {
                    if (System.IO.File.Exists(fileName))
                        System.IO.File.Delete(fileName);
                }
                doh.Reset();
                doh.SqlCmd = "SELECT Id, ParentId FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + ChannelId + " AND [Id]=" + _classId;
                DataTable dtClass = doh.GetDataTable();
                if (dtClass.Rows.Count > 0 && dtClass.Rows[0]["ParentId"].ToString() != "0" && DeleteParent == true)
                {
                    DeleteClassFile(dtClass.Rows[0]["ParentId"].ToString(), true);
                }
                dtClass.Clear();
                dtClass.Dispose();
            }

        }
        /// <summary>
        /// 生成栏目文件
        /// </summary>
        /// <param name="_classId"></param>
        /// <param name="CreateParent"></param>

        protected void CreateClassFile(string _classId, bool CreateParent)
        {
            TemplateEngine te = new TemplateEngine(doh, ChannelId);
            JumbotCms.Common.DataPageCount dtp = new JumbotCms.Common.DataPageCount(doh, ChannelId);
            int pageCount = dtp.Class(_classId, true);//栏目页总页数
            int maxPage = Jumbot.Common.Int.Min(site.CreatePages, pageCount);
            string PageStr = string.Empty;
            for (int i = 1; i < (maxPage + 1); i++)
            {
                PageStr = te.GetClassStr(_classId, i);
                Jumbot.Common.DirFile.SaveFile(PageStr, Go2Class(doh, i, true, ChannelId, _classId));
            }
            doh.Reset();
            doh.SqlCmd = "SELECT Id, ParentId FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + ChannelId + " AND [Id]=" + _classId;
            DataTable dtClass = doh.GetDataTable();
            if (dtClass.Rows.Count > 0 && dtClass.Rows[0]["ParentId"].ToString() != "0" && CreateParent == true)
            {
                CreateClassFile(dtClass.Rows[0]["ParentId"].ToString(), true);
            }
            dtClass.Clear();
            dtClass.Dispose();

        }
        /// <summary>
        /// 删除内容文件
        /// </summary>

        protected void DeleteContentFile(string _contentID)
        {
            IModule module = (IModule)Activator.CreateInstance(Type.GetType(String.Format("JumbotCms.API.M{0}", ChannelType), true, true));
            module.DeleteContent(doh, ChannelId, _contentID);
        }
        /// <summary>
        /// 生成内容页
        /// </summary>
        /// <param name="_contentID">内容ID</param>
        /// <param name="_currentPage">指定的页码,-1表示所有</param>
        protected void CreateContentFile(string _contentID, int _currentPage)
        {
            IModule module = (IModule)Activator.CreateInstance(Type.GetType(String.Format("JumbotCms.API.M{0}", ChannelType), true, true));
            module.CreateContent(doh, ChannelId, _contentID, _currentPage);
        }
        #endregion
    }
}